import { _decorator, Component, Node, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('BgScroll')
export class BackgroundScroller extends Component {
    @property(Node)
    bg1: Node = null!;

    @property(Node)
    bg2: Node = null!;

   
    scrollSpeed: number = 100; // 每秒滚动速度（像素/秒）

    private bgWidth = 720;

    update(dt: number) {
        // 滚动两张图
        this.bg1.setPosition(this.bg1.position.x - this.scrollSpeed * dt, 0, 0);
        this.bg2.setPosition(this.bg2.position.x - this.scrollSpeed * dt, 0, 0);

        // 如果 bg1 完全离开屏幕左侧，则移到 bg2 的右边
        if (this.bg1.position.x <= -this.bgWidth) {
            this.bg1.setPosition(this.bg2.position.x + this.bgWidth, 0, 0);
        }

        // 同理，bg2移出后，移到 bg1 的右边
        if (this.bg2.position.x <= -this.bgWidth) {
            this.bg2.setPosition(this.bg1.position.x + this.bgWidth, 0, 0);
        }
    }

    onRestartGame(){
        this.bg1.setPosition(0 , 0, 0);
        this.bg2.setPosition(720, 0, 0);


    }
}
